import request from "@/utils/request";
import { IObject } from "../common/common";

const BASE_URL = "/business/reimburse";

const ReimburseAPI = {
  /** 获取分页数据 */
  getPage(params?: ReimbursePageQuery) {
    return request<any, PageResult<ReimbursePageVO[]>>({
      url: `${BASE_URL}/loadPage`,
      method: "get",
      params,
    });
  },
  /**
   * 根据id查询
   */
  load(id: string) {
    return request<any, ReimburseDetail>({
      url: `${BASE_URL}/load/${id}`,
      method: "get",
    });
  },
  /**
   * 创建报销申请
   * @param data 表单数据
   */
  create(data: ReimburseForm) {
    return request<any, ReimbursePageVO>({
      url: `${BASE_URL}/add`,
      method: "post",
      data,
    });
  },
  /**
   * 修改报销申请
   * @param data 表单数据
   */
  update(data: ReimburseForm) {
    return request<any, ReimbursePageVO>({
      url: `${BASE_URL}/update`,
      method: "post",
      data,
    });
  },
  /**
   * 删除报销申请
   */
  deleteById(id: string) {
    return request({
      url: `${BASE_URL}/delete/${id}`,
      method: "get",
    });
  },
  /**
   * 启动报销流程
   */
  start(params: { id: string; flowId: string }) {
    return request({
      url: `${BASE_URL}/startActivity`,
      method: "get",
      params,
    });
  },
  /**
   * 采购申请单审核
   */
  review(data: ReimburseReviewVO) {
    return request<ReimburseReviewVO, any>({
      url: `${BASE_URL}/review`,
      method: "post",
      data,
    });
  },

  /**
   * 采购申请单配送
   */
  apply(data: ReimburseApplyVO) {
    return request<ReimburseApplyVO, any>({
      url: `${BASE_URL}/apply`,
      method: "post",
      data,
    });
  },
};

export default ReimburseAPI;

/** 筛选条件 */
export interface ReimbursePageQuery extends PageQuery {
  /** 状态 */
  statuses?: string;
  /** 类型 */
  type?: string;
}

/** 实例分页对象 */
export interface ReimbursePageVO {
  /** ID */
  id: string;
  /** 流程模板ID */
  activityId: string;
  /** 采购单ID */
  purchaseId: string;
  contractId: string;
  /** 名称 */
  title: string;
  /** 描述 */
  remark: string;
  /** 报销金额 */
  amount: number;
  /** 总金额 */
  totalAmount: string;
  /** 创建时间 */
  createTime: string;
  /** 申报人 */
  userId: string;
  username: string;
  /** 类型 */
  type: string;
  /** 状态 */
  status: string;
}

export interface ReimburseDetail extends ReimbursePageVO {
  /** 报销申请单 */
  items: ReimburseItemVO[];
  /** 最新的申请记录 */
  lastRecord?: ReimburseRecord;
  itemTypeAmounts: ItemTypeAmount[];
}

export interface ItemTypeAmount {
  type: string;
  typeName: string;
  contractAmount: number;
  applyAmount: number;
  compare: number;
}

export interface ReimburseForm {
  /** ID */
  id?: string;
  /** 合同ID */
  contractId: string;
  /** 采购单ID */
  purchaseId: string;
  /** 名称 */
  title: string;
  /** 描述 */
  remark?: string;
  /** 报销金额 */
  amount: string;
  type: string;
  /** 报销申请单 */
  items: ReimburseItemVO[];
}

export interface ReimburseItemVO {
  /** 节点ID */
  id?: string;
  /** 关联申购id */
  reimburseId?: string;
  /** 名称 */
  name?: string;
  /** 描述 */
  remark?: string;
  /** 报销金额 */
  amount: number | string;
  /** 数量 */
  quantity: number;
  spec?: string;
  unit?: string;
  code?: string;
  materialId: string;
  materialType: string;
  /** 冗余字段 */
  activityId?: string;
  taskId?: string;
  taskName?: string;
  normId?: string;
  normName?: string;
  username?: string; //工人名称
  taskGyDispatchIds?: string[];
  reimburseItemTasks?: ReimburseItemTaskVO[];
  /* 已申报 */
  totalAmount?: string;
  totalQuantity?: number;
  /* 合同数量金额 */
  contractPrice?: string;
  contractQuantity?: number;
}

export interface ReimburseItemTaskVO {
  /** ID */
  id: string;
  reimburseId: string;
  reimburseItemId: string;
  taskGyDispatchId: string;
  name: string; //工艺名称
  taskId: string;
  taskName: string;
  userId: string;
  username: string;
  normId: string;
  normName?: string;
}

export enum ReimburseStatus {
  CREATE = "CREATE",
  APPLY_BACK = "APPLY_BACK",
  APPLY = "APPLY",
  END = "END",
}

export enum ReimburseType {
  MAIN = "MAIN",
  SUB = "SUB",
  NORM = "NORM",
  INSTRUMENT = "INSTRUMENT",
}

export const Reimburse_Status_Label: IObject = {
  [ReimburseStatus.CREATE]: "创建中",
  [ReimburseStatus.APPLY_BACK]: "驳回",
  [ReimburseStatus.APPLY]: "申请中",
  [ReimburseStatus.END]: "结束",
};

export const Reimburse_Type_Label: IObject = {
  [ReimburseType.MAIN]: "主材",
  [ReimburseType.SUB]: "辅材",
  [ReimburseType.NORM]: "人工",
  [ReimburseType.INSTRUMENT]: "器具",
};

export const Reimburse_Apply_Label: IObject = {
  ["0"]: "未审核",
  ["1"]: "通过",
  ["2"]: "驳回",
};

/** 申请表单 */
export interface ReimburseApplyVO {
  /** ID */
  id: string;
}

/** 审核表单 */
export interface ReimburseReviewVO {
  /** ID */
  recordId: string;
  /** 审核结果 */
  result: string;
  /** 审批说明 */
  reviewMark: string;
}

/** 采购申请单 */
export interface ReimburseRecord {
  /** ID */
  id: string;
  /** 采购申请单 */
  purchaseId: string;
  /** 申请人id */
  applyUserId: string;
  /** 申请人 */
  applyUsername: string;
  /** 申请时间 */
  applyTime: string;
  /** 申请说明 */
  applyMark: string;
  /** 审核人id */
  reviewUserId: string;
  /** 审核人 */
  reviewUsername: string;
  /** 审核时间 */
  reviewTime: string;
  /** 审核说明 */
  reviewMark: string;
  /** 审核结果 0: 未审批 1: 通过 2: 未通过 */
  result: number;
}
